package cn.edu.zut.soft.student.controller.admin;

import cn.edu.zut.soft.student.bean.ResultBean;
import cn.edu.zut.soft.student.model.Notice;
import cn.edu.zut.soft.student.service.notice.NoticeBo;
import cn.edu.zut.soft.student.service.notice.vo.NoticeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ChrisPhantom
 * @CreatDate 2021/02/18 16:31
 */

@RestController
@RequestMapping("/admin/api/notice")
public class NoticeController {
    @Autowired
    private NoticeBo noticeBo;

    @PostMapping("")
    public ResultBean<Integer> addNotice(Notice notice) {
        return new ResultBean<>(noticeBo.addNotice(notice));
    }

    @DeleteMapping("/{noticeid}")
    public ResultBean<Integer> deleteNotice(@PathVariable("noticeid") Integer noticeid) {
        return new ResultBean<>(noticeBo.deleteNotice(noticeid));
    }

    @DeleteMapping("/list")
    public ResultBean<Integer> deleteNotices(String allNoticeIds) {
        String[] ids = allNoticeIds.substring(1, allNoticeIds.length() - 1).split(",");
        for (String id : ids) {
            noticeBo.deleteNotice(Integer.parseInt(id));
        }
        return new ResultBean<>();
    }

    @PutMapping("")
    public ResultBean<Integer> updateNotice(Notice notice) {
        return new ResultBean<>(noticeBo.updateNotice(notice));
    }

    @GetMapping("/{noticeid}")
    public ResultBean<Notice> queryNotice(@PathVariable("noticeid") Integer noticeid) {
        return new ResultBean<>(noticeBo.getNoticeById(noticeid));
    }

    @GetMapping("")
    public ResultBean<List<Notice>> queryAllNotices(NoticeQuery noticeQuery) {
        return new ResultBean<>(noticeBo.getNotices(noticeQuery));
    }
}
